package com.example.mypotiota_si.Activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.mypotiota_si.R;
import com.example.mypotiota_si.Tool.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class SouSuoActivity extends Activity {
    private static final String TAG = "SouSuoActivity";
    ListView ss_listView;
    SearchView ss_searchView;
    ImageView ss_fh;
    ArrayList<String> data=new ArrayList<>();
    private ArrayList<String> strings=null;
    private ArrayAdapter<String> stringArrayAdapter;
    TextView textView;
//    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_ss_list_view);
        //拿数据
        init_getdate();
//        sharedPreferences = getSharedPreferences("shop",MODE_PRIVATE);
        //隐藏标题栏
//        ActionBar actionBar = getSupportActionBar();
//        if (actionBar != null) {
//            actionBar.hide();
//        }
        //找到控件
        ss_listView=findViewById(R.id.ss_listView);
        ss_searchView=findViewById(R.id.ss_searchView);
        ss_fh=findViewById(R.id.ss_fh);
        textView=findViewById(R.id.start_search);
        //点击返回
        init_dj_fh();
        //点击搜索方法
        dianji_sousuokongjian();

    }
    //点击搜索方法
    private void dianji_sousuokongjian() {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(strings==null){
                    Toast.makeText(SouSuoActivity.this, "数据加载中......", Toast.LENGTH_SHORT).show();
                }else {
                    //获取id  根据ss_searchview这个控件获取它内部的这个编辑框----然后把这个编辑框的id值拿出来
                    int id = ss_searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
                    //这里把编辑框的id值绑定到新的编辑框上，然后就可以操作这个编辑框
                    EditText textView = (EditText) ss_searchView.findViewById(id);

                    if (textView.getText().toString().equals("")) {
                    } else {
                        stringArrayAdapter.getFilter().filter(textView.getText().toString());
                        if (stringArrayAdapter.getCount() == 0) {
                            Toast toast = Toast.makeText(SouSuoActivity.this, "找不到相关内容", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                        } else {
                            ss_listView.setAdapter(stringArrayAdapter);
                        }
                    }
                }
            }
        });


    }
    //拿数据
    private void init_getdate() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                strings=new ArrayList<>();
                String date= Tools.getJson("json.json",SouSuoActivity.this);
                try {
                    JSONObject jsonObject=new JSONObject(date);
                    String code=jsonObject.getString("code");
                    JSONArray shop_dates=jsonObject.getJSONArray("shop_dates");
                    for (int i = 0; i < shop_dates.length(); i++) {
                        JSONObject jsonObject1=shop_dates.getJSONObject(i);
                        JSONArray date1=jsonObject1.getJSONArray("date");
                        for (int i1 = 0; i1 < date1.length(); i1++) {
                            JSONObject jsonObject2=date1.getJSONObject(i1);
                            String shop_name=jsonObject2.getString("shop_name");
                            Log.i(TAG,shop_name+"");
                            strings.add(shop_name);
                        }
                    }
                    init_setSearchWay();
                } catch (JSONException e) {
                    Log.i(TAG,"错误"+e);
                    e.printStackTrace();
                }

                for (int i = 0; i < strings.size(); i++) {
                    Log.i(TAG, "dates  ++++++++: "+strings.get(i));
                }
            }
        }).start();

    }

//点击返回
    private void init_dj_fh() {
        ss_fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //设置搜索方法
    private void init_setSearchWay() {
        ss_searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出软键盘
                ss_searchView.setIconified(false);
            }
        });

        stringArrayAdapter = new ArrayAdapter<String>(SouSuoActivity.this, android.R.layout.simple_list_item_1,strings);

        ss_searchView . setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                int wdawa = 0;
                if (!TextUtils.isEmpty(newText)){

                    stringArrayAdapter.getFilter().filter(newText);

                }else{
                    ss_listView.clearTextFilter();
                    stringArrayAdapter.getFilter().filter(newText.toString());
                }
                return true;
            }
        });
    }
}
